package com.imageprocess.tools;

import java.nio.IntBuffer;

import jjil.core.RgbImage;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;

public class ImageHelper {
 
    // 从16进制颜色获取颜色通道
    private static int getR32(int c) {
        return (c >> 0) & 0xFF;
    }

    private static int getG32(int c) {
        return (c >> 8) & 0xFF;
    }

    private static int getB32(int c) {
        return (c >> 16) & 0xFF;
    }

    private static int getA32(int c) {
        return (c >> 24) & 0xFF;
    }

    // 不考虑Alpha通道
    private static int pack8888(int r, int g, int b, int a) {
        return (r << 0) | (g << 8) | (b << 16) | (a << 24);
    }

    /**
     * 图片合成
     * 
     * @param bitmap
     * @return
     */
    public static Bitmap createBitmap(Bitmap src, Bitmap watermark) {
        if (src == null) {
            return null;
        }
        int w = src.getWidth();
        int h = src.getHeight();
        int ww = watermark.getWidth();
        int wh = watermark.getHeight();
        // create the new blank bitmap
        Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        Canvas cv = new Canvas(newb);
        // draw src into
        cv.drawBitmap(src, 0, 0, null);// 在 0，0坐标开始画入src
        // draw watermark into
        cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印
        // save all clip
        cv.save(Canvas.ALL_SAVE_FLAG);// 保存
        // store
        cv.restore();// 存储
        return newb;
    }

    /**
     * 图片圆角
     * 
     * @param bitmap
     * @return
     */
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = 12;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    /**
     * 缩放、翻转和旋转图片
     * 
     * @param bmpOrg
     * @param rotate
     * @return
     */
    public static Bitmap gerZoomRotateBitmap(Bitmap bmpOrg, float sw, float sh) {
        // 获取图片的原始的大小
        int width = bmpOrg.getWidth();
        int height = bmpOrg.getHeight();

        // // 定义缩放的高和宽的比例
        // float sw = -1;
        // float sh = -1;
        // 创建操作图片的用的Matrix对象
        android.graphics.Matrix matrix = new android.graphics.Matrix();
        // 缩放翻转图片的动作
        // sw sh的绝对值为绽放宽高的比例，sw为负数表示X方向翻转，sh为负数表示Y方向翻转
        matrix.postScale(sw, sh);

        // 创建一个新的图片
        android.graphics.Bitmap resizeBitmap = android.graphics.Bitmap
                .createBitmap(bmpOrg, 0, 0, width, height, matrix, true);
        return resizeBitmap;
    }

    public static Bitmap rotateBitmap(Bitmap bmpOrg, int rotate) {
        int width = bmpOrg.getWidth();
        int height = bmpOrg.getHeight();
        // 创建操作图片的用的Matrix对象
        android.graphics.Matrix matrix = new android.graphics.Matrix();
        matrix.postScale(1, 1);
        // 旋转30*
        matrix.setRotate(rotate);
        // 创建一个新的图片
        android.graphics.Bitmap resizeBitmap = android.graphics.Bitmap
                .createBitmap(bmpOrg, 0, 0, width, height, matrix, true);
        return resizeBitmap;
    }

    private static void setTranslate(ColorMatrix cm, float dr, float dg,
            float db, float da) {
        cm.set(new float[] { 2, 0, 0, 0, dr, 0, 2, 0, 0, dg, 0, 0, 2, 0, db, 0,
                0, 0, 1, da });
    }

    private static void setContrast(ColorMatrix cm, float contrast) {
        float scale = contrast + 1.f;
        float translate = (-.5f * scale + .5f) * 255.f;
        cm.set(new float[] { scale, 0, 0, 0, translate, 0, scale, 0, 0,
                translate, 0, 0, scale, 0, translate, 0, 0, 0, 1, 0 });
    }

    private static void setContrastTranslateOnly(ColorMatrix cm, float contrast) {
        float scale = contrast + 1.f;
        float translate = (-.5f * scale + .5f) * 255.f;
        cm.set(new float[] { 1, 0, 0, 0, translate, 0, 1, 0, 0, translate, 0,
                0, 1, 0, translate, 0, 0, 0, 1, 0 });
    }

    private static void setContrastScaleOnly(ColorMatrix cm, float contrast) {
        float scale = contrast + 1.f;
        float translate = (-.5f * scale + .5f) * 255.f;
        cm.set(new float[] { scale, 0, 0, 0, 0, 0, scale, 0, 0, 0, 0, 0, scale,
                0, 0, 0, 0, 0, 1, 0 });
    }

    public static Bitmap colorMatrixEffect1(Bitmap bmp) {
        Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        ColorMatrix cm = new ColorMatrix();
        float mAngle = 100;
        float contrast = mAngle / 180.f;
        setContrast(cm, contrast);
        mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
        Canvas canvas = new Canvas(bmp);
        canvas.drawBitmap(bmp, 0, 0, mPaint);
        return bmp;
    }

    public static Bitmap colorMatrixEffect2(Bitmap bmp) {
        Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        ColorMatrix cm = new ColorMatrix();
        float mAngle = 100;
        float contrast = mAngle / 180.f;
        setContrast(cm, contrast);
        mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
        Canvas canvas = new Canvas(bmp);
        canvas.drawBitmap(bmp, 0, 0, mPaint);
        return bmp;
    }

    public static Bitmap colorMatrixEffect3(Bitmap bmp) {
        Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        ColorMatrix cm = new ColorMatrix();
        float mAngle = 100;
        float contrast = mAngle / 180.f;
        setContrast(cm, contrast);
        mPaint.setColorFilter(new ColorMatrixColorFilter(cm));
        Canvas canvas = new Canvas(bmp);
        canvas.drawBitmap(bmp, 0, 0, mPaint);

        return bmp;
    }

    public static void greyScale(Bitmap map) {
        int width = map.getWidth();
        int height = map.getHeight();
        int n = width * height;
        IntBuffer buffer = IntBuffer.allocate(n);
        map.copyPixelsToBuffer(buffer);
        // int[] rgbData=buffer.array();
        for (int index = 0; index < n; index++) {
            int rgb = buffer.get(index);
            // int R = ((rgbData[index] >> 16) & 0xff);
            // int G = ((rgbData[index] >> 8) & 0xff);
            // int B = ((rgbData[index]) & 0xff);
            int R = getR32(rgb);
            int G = getG32(rgb);
            int B = getB32(rgb);
            int A = getA32(rgb);
            int grey = (R + G + B + A) / 4;

            // rgbData[index] = 0xff000000 | (grey << 16) | (grey << 8) |
            // grey;
            int c = pack8888(grey, grey, grey, A);
            buffer.put(index, c);

        }
        // buffer=makeBuffer(rgbData,n);
        // buffer = IntBuffer.wrap(rgbData);
        map.copyPixelsFromBuffer(buffer);

    }

    public static void colorFilter(Bitmap map) {
        int width = map.getWidth();
        int height = map.getHeight();
        int n = width * height;
        IntBuffer buffer = IntBuffer.allocate(n);
        map.copyPixelsToBuffer(buffer);
        for (int index = 0; index < n; index++) {
            int rgb = buffer.get(index);

            int R = getR32(rgb);
            int G = 0;
            int B = getB32(rgb);

            int c = pack8888(R, G, B, 255);
            buffer.put(index, c);

        }

        map.copyPixelsFromBuffer(buffer);
    }

   
}
